//匿名函数,页面加载完成后调用
$(function () {
    $("#topBtn").click(setTop);
    $("#wonderfulBtn").click(setWonderful);
    $("#blockBtn").click(setBlock);
});

//置顶
function setTop() {
    $.post(CONTEXT_PATH + "/discuss/top",
        {"id": $("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code == 0) {
                $("#topBtn").attr("disabled", "disabled");
            } else {
                alert(data.msg);
            }
        }
    );
}


//加精
function setWonderful() {
    $.post(CONTEXT_PATH + "/discuss/wonderful",
        {"id": $("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code == 0) {
                $("#wonderfulBtn").attr("disabled", "disabled");
            } else {
                alert(data.msg);
            }
        }
    );
}


//删除
function setBlock() {
    $.post(CONTEXT_PATH + "/discuss/block",
        {"id": $("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code == 0) {
                location.href = CONTEXT_PATH+"/index";
            } else {
                alert(data.msg);
            }
        }
    );
}


function like(btn, entityType, entityId, entityUserId, postId) {
    $.ajax(CONTEXT_PATH + "/like", {
        method: "POST",
        data: {entityType: entityType, entityId: entityId, entityUserId: entityUserId, postId: postId},
        success: function (result) {
            result = $.parseJSON(result);

            if (result.code == 0) {
                $(btn).children("i").text(result.likeCount);
                $(btn).children("b").text(result.likeStatus == 1 ? '已赞' : '赞');
            } else {
                alert(result.msg);
            }
        }
    });

}